package com.shenghui.enterprise.base.controller;

import com.shenghui.enterprise.base.entity.BaseModule;
import com.shenghui.enterprise.base.entity.BaseUser;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.utils.UserUtil;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.service.IBaseModuleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 模块 前端控制器
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@RestController
@RequestMapping("/admin/baseModule" )
@Api(value = "模块接口",description = "用作模块演示")
public class BaseModuleController {
    @Autowired
    private IBaseModuleService baseModuleService;

    @Autowired
    private UserUtil userUtil;

    /**
     * 查询列表信息

     * @return
     */
    @RequestMapping("/getBaseModuleTreeList")
    @ResponseBody
    public Object getBaseModuleTreeList(@RequestParam HashMap map){
        return baseModuleService.getBaseModuleTreeList(map);
    }

    /**
     * 删除模块信息
     * @param id
     * @return
     */
    @RequestMapping("/deleteBaseModule")
    public Result deleteBaseModule(String id){
        if(GeneralUtil.isNull(id)){
            return Result.error("参数【id】不能为空");
        }
        Result result = baseModuleService.deleteBaseModule(id);
        return result;
    }

    /**
     * 修改模块排序值
     * @return
     */
    @RequestMapping("/updateBaseModuleOrder")
    @ResponseBody
    public Object updateBaseModuleOrder(String id, Integer orderValue){
        return baseModuleService.updateBaseModuleOrder(id, orderValue);
    }

    /**
     * 添加模块信息
     * @return
     */
    @RequestMapping("/addBaseModule")
    @ResponseBody
    public Result addBaseModule(BaseModule module, String id){
        Result result = baseModuleService.addBaseModule(module, id);
        return result;
    }

    /**
     * 根据id查询模块详情信息
     * @param id
     * @return
     */
    @RequestMapping("/getBaseModuleById")
    @ResponseBody
    public Result getBaseModuleById(String id){
        if(StringUtils.isEmpty(id) || id.equals("0")){
            return Result.error("必填参数【id】不能为空");
        }
        Map<String,Object> module = baseModuleService.getBaseModuleById(id);
        if(GeneralUtil.isNotNull(module)){
            return Result.ok("查询成功", module);
        } else {
            return Result.error("未查询到对应的模块记录");
        }
    }

    /**
     * 获取顶级模块
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/topModule")
    public Object topModule(HttpServletRequest request, HttpServletResponse response) {
        BaseUser baseUser = userUtil.getLoginUser();
        if (baseUser != null) {
            Map _map = new HashMap();
            _map.put("userId", baseUser.getUserId());
            return baseModuleService.topModule(_map, request, response);
        }
        return null;
    }

    /**
     * 获取子集模块
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/childModule")
    public Object childsModule(String parentId, HttpServletRequest request, HttpServletResponse response ) {
        return baseModuleService.childsModule(parentId, request, response);
    }
}
